﻿<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试</title>
  </head>
  <body>
    <button id="btn">点我</button>
    <div id="val"></div>
    <div id="code"></div>
    <div id="pay"></div>
  </body>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
      $("#val").html(location.href);
      var btn = document.getElementById("btn");
      var code = "";
      if (is_weixn()) {
        code = getQueryVariable("code");
        $("#code").html(code);
        $.ajax({
          type: "GET", //提交方式
          url: "http://jyapi.nonghanxue.com/api/WxAuth/SaveOpenId", //路径
          // contentType: "application/json",
          // dataType: "json",
          data: { code: code },
          headers: {
            token: "28732617efe7471cb3228711dc83bb79",
          },
          success: function (res) {
            alert(res.data);
          },
        });
      }

      document.addEventListener(
        "WeixinJSBridgeReady",
        function onBridgeReady() {
          alert("1111111111");
          $.ajax({
            type: "POST", //提交方式
            url: "http://jyapi.nonghanxue.com/api/Order/Buy", //路径
            contentType: "application/json",
            dataType: "json",
            headers: {
              token: "28732617efe7471cb3228711dc83bb79",
            },
            data: JSON.stringify({
              courseId: 16,
              discountsId: 0,
              invoiceType: 1,
              invoiceHeader: "玫瑰花",
              invoiceTaxpayer: "123",
              invoiceContent: "123",
              code: code,
            }), //数据，这里使用的是Json格式进行传输
            success: function (result) {
              //返回数据根据结果进行相应的处理
              btn.onclick = function () {
                $("#pay").html(JSON.stringify(result.data));
                WeixinJSBridge.invoke(
                  "getBrandWCPayRequest",
                  {
                    appId: result.data.appId, //公众号名称，由商户传入
                    timeStamp: result.data.timeStamp, //时间戳
                    nonceStr: result.data.nonceStr, //随机串
                    package: result.data.package, //扩展包
                    signType: result.data.signType, //微信签名方式:MD5
                    paySign: result.data.paySign, //微信签名
                  },
                  function (res) {
                    alert(JSON.stringify(res));
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                      if (confirm("支付成功！点击“确定”进入退款流程测试。")) {
                        location.href = '@Url.Action("Refund", "TenPayV3")';
                      }
                      //console.log(JSON.stringify(res));
                    }
                    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    //因此微信团队建议，当收到ok返回时，向商户后台询问是否收到交易成功的通知，若收到通知，前端展示交易成功的界面；若此时未收到通知，商户后台主动调用查询订单接口，查询订单的当前状态，并反馈给前端展示相应的界面。
                  }
                );
              };
            },
          });
        },
        false
      );

      btn.onclick = function () {
        if (is_weixn()) {
        } else {
          $.ajax({
            type: "POST", //提交方式
            url: "http://jyapi.nonghanxue.com/api/Order/Buy", //路径
            contentType: "application/json",
            dataType: "json",
            headers: {
              token: "28732617efe7471cb3228711dc83bb79",
            },
            data: JSON.stringify({
              courseId: 16,
              discountsId: 0,
              invoiceType: 1,
              invoiceHeader: "玫瑰花",
              invoiceTaxpayer: "123",
              invoiceContent: "123",
              code: code,
            }), //数据，这里使用的是Json格式进行传输
            success: function (result) {
              //返回数据根据结果进行相应的处理
              if (result.data.isWxBrowser) {
              } else {
                location.href = result.data.mWebUrl;
              }
            },
          });
        }
      };
    });

    // /api/WxAuth/SaveOpenId
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return false;
    }

    function is_weixn() {
      var ua = navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
      } else {
        return false;
      }
    }
  </script>
</html>
